﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChgDoc.Entity;
using System.Reflection;
using System.Collections.Concurrent;

namespace ChgDoc.Edit
{
	[AdminPage( "Edit template" , "Admin")]
	public partial class TemplateEdit : BasePage
	{
		protected override IEnumerable<FormRule> GetFormRules()
		{
			return new List<FormRuleMapping<Template>>()
		                {
		                    new FormRuleMapping<Template>( txtName, "Name", FormRuleCondition.NotNull, (s,v) => s.Name = (string)v),
                            new FormRuleMapping<Template>( txtBody, "Body", FormRuleCondition.None, (s,v) => s.Body = (string)v),
                            new FormRuleMapping<Template>( drpList, "Eng", FormRuleCondition.None, (s,v) => s.RenderEngineCode = (string)v),
                            new FormRuleMapping<Template> (chkSubtemplate, "", FormRuleCondition.None,  (s,v) => s.IsChild = (bool)v)
		                };
		}
		
		protected void Page_Load( object sender, EventArgs e )
		{
            btnSaveView.Visible = (Request.QueryString["ReturnUrl"] != null);

			if (IsPostBack)
				return;

            drpList.DataSource = RenderEngine.ReadAll();
            drpList.DataValueField = "Code";
            drpList.DataTextField = "DisplayName";
            drpList.DataBind();

			if (!HasParam( "Id" ))
				return;

			Template load = Template.Read( LongParam( "Id" ) );
			txtName.Text = load.Name;
			txtBody.Text = load.Body;
            drpList.SelectedValue = load.RenderEngineCode;
            chkSubtemplate.Checked = load.IsChild;
		}

        protected void btnSave_click(object sender, EventArgs e)
        {
            SaveModel();
            ClearCaches();
        }

        protected void btnSaveView_click(object sender, EventArgs e)
        {
            SaveModel();
            ClearCaches();
            DoReturnUrl();
        }

        private static void ClearCaches()
        {
            Config.ClearCaches();

        }

        private void SaveModel()
        {
            Template model = new Template() { Id = LongParam("Id") };

            if (!PopulateModelFromControls<Template>(model))
                return;

            model.Save();
        }
	}
}